<?php
namespace app\index\controller;

use app\common\model\Company as CompanyModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Recharge as RechargeModel;
/**
 * 寺院管理控制器
 * Class Index
 * @package app\index\controller
 */
class Company extends Base
{
    /**
     * [index  获取寺院数据]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $user = $this->user;
        $model = new CompanyModel;
        try{
            $company = $model->findData($user['comid'],'id','id,comname,title,compic,province,city,qu,addr,imgs,username,phone,jieyuan_isshow,content,class_phone,bj_music,notice');
            $company['content'] = get_img_thumb_url(percent1($company['content']),config('domain_name').'/');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }
            if(empty($company['notice'])){
                $company['notice'] = '佛事期间，敬请各斋主、信众衣着装束严整庄严，殿堂内勿喧哗、闲聊、吸烟，佛事过程中将手机声音关闭。如确需谈话、接听电话请至殿堂之外。';
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看寺院设置');
        return admin_json(1,'获取成功',$company);
    }
    /**
     * [index  修改寺院数据]
     * @return json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new CompanyModel;
        try{
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'修改寺院设置');
        return admin_json(1,'修改成功');
    }
    /**
     * [index  获取寺院名数据]
     * @return json
     * @author [C。]
     */
    public function getName()
    {
        $param = input('param.');
        $model = new CompanyModel();
        $gmodel = new CompanyGongdengOrderModel;
        $rmodel = new RechargeModel;
        try{
            $company = $model->findData($param['comid'],'id','comname,compic,title,read_num');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }

            $gongdeng = $gmodel->getSum(['status'=>1,'comid'=>$param['comid']],'total_price');
            $act = $rmodel->getSum(['comid'=>$param['comid'],'states'=>1],'amt');
            $allprice = round($act + $gongdeng, 2);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',['company'=>$company,'allprice'=>$allprice]);
    }
    /**
     * [modules  获取寺院功能菜单]
     * @return json
     * @author [C。]
     */
    public function modules()
    {
        $model = new CompanyModel();
        $user = $this->user;
        try{
            $data = [];
            $i = 0;
            $company = $model->findData($user['comid'],'id','modules');
            if(!empty($company)){
                $company['modules'] = unserialize($company['modules']);
                foreach($company['modules'] as $k=>$v){
                    if($v['is_show'] == 1){
                        $data[$i] = $v;
                        $i++;
                    }
                }
                $company['modules'] = $data;
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',$company);
    }
}